<template>
  <div class="bg">
    <div class="login-container">
      <div class="title-text">旅游管理系统</div>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        v-if="xianshi">
        <el-form-item label="用户名" prop="pass">
          <el-input v-model="ruleForm.pass" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密&nbsp;&nbsp;&nbsp;&nbsp;码" prop="checkPass">
          <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item class="demo-ruleForm__btn">
          <el-button type="primary" @click="submitForm(ruleFormRef)" >登录</el-button>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        v-if="!xianshi">
        <el-form-item label="用户名" prop="pass">
          <el-input v-model="ruleForm.pass" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密&nbsp;&nbsp;&nbsp;&nbsp;码" prop="checkPass">
          <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="再次密码" prop="checkPass">
          <el-input v-model="ruleForm.checkPass1" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item class="demo-ruleForm__btn">
          <el-button type="primary" @click="submit(ruleFormRef)" >注册</el-button>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <span @click="qiehaun">->登录</span>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
const xianshi = ref(true);

const router = useRouter();
//注册
function submit(){
  people.push(
    {
      name:ruleForm.pass,
      password:ruleForm.checkPass
    }
  )
}
function qiehaun() {
  xianshi.value = !xianshi.value;
}
const people = reactive([
    {
      name: 'admin',
      password: '123456',
    },
    {
      name: 'Tom',
      password: '123',
    },
  ])
//是否输入验证
function submitForm() {
  const found = people.find(
    (user) => user.name === ruleForm.pass && user.password === ruleForm.checkPass
  );
  if (found) {
    router.push("/index/shouye");
  } else {
    alert("用户名或密码错误，请重新输入！");
  }
}
const checkAge = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("Please input the age"));
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error("Please input digits"));
    } else {
      if (value < 18) {
        callback(new Error("Age must be greater than 18"));
      } else {
        callback();
      }
    }
  }, 1000);
};

const ruleForm = reactive({
  pass: "",
  checkPass: "",
  age: "",
});
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const rules = reactive({
  pass: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
  age: [{ validator: checkAge, trigger: "blur" }],
});
//清空按钮
const resetForm = () => {
  ruleForm.pass = ''
  ruleForm.checkPass = ''
};
</script>
<style scoped>
.title-text {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
}

.bg{
  width: 100%;
  height: 1100px;
  background-image: url('../assets/img/login.jpg');
  background-size: cover;
  background-position: center;
}
.el-form-item__content {
    align-items: center;
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    justify-content: space-around;
}
.login-container {
  border: 1px solid #dcdfe6; 
    /* box-shadow: 2px 2px 2px 12px rgba(0, 0, 0, 0.1); */
    opacity: 0.8;
    border-radius: 10%;
    background: #ffffff;
    width: 500px;
    height: 500px;
    position: fixed;
    right: 600px;
    top: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: stretch;
    flex-wrap: wrap;
    flex-direction: column;
}
</style>
